use crate::model::satori::SatoriUser;
use sqlx::{Error, Pool, Postgres};
use uuid::Uuid;
use void_log::log_info;
use crate::model::sys_user;

impl SatoriUser {
    pub async fn select(pool: &Pool<Postgres>, id: &Uuid) -> Result<SatoriUser, Error> {
        log_info!("[ Satori ] 按 Id: {id} 查询用户");
        let sql = "SELECT * FROM satori.satori_user where id = $1";
        sqlx::query_as::<_, Self>(sql).bind(id).fetch_one(pool).await
    }

    pub async fn select_username(pool: &Pool<Postgres>, username: &str) -> Result<SatoriUser, Error> {
        log_info!("按 Name: {username} 查询用户");
        // 分离账户
        let (account, code) = sys_user::get_account_code(username);
        // 查询账户
        let sql = "select b.* from sys_user a, satori.satori_user b where a.id = b.id and a.code = $1 and a.account = $2";
        sqlx::query_as::<_, Self>(sql).bind(code).bind(account).fetch_one(pool).await
    }
}